﻿using B.S.Medical.Api.Read.Application.Command.Register;
using B.S.Medical.Domain.Doctor;
using B.S.Medical.ErrorCode;
using B.S.Medical.Infrastructure.Interface;
using MediatR;

namespace B.S.Medical.Api.Read.Application.Handler.Register
{
    /// <summary>
    /// 预约须知查询实现
    /// </summary>
    public class AppointmentNoticeHandler : IRequestHandler<AppointmentNoticeCommand, ApiResult<AppointmentNoticeModel>>
    {
        private readonly IBaseRepository<AppointmentNoticeModel> appointmentNoticeRepo;
        private readonly ILogger<AppointmentNoticeHandler> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AppointmentNoticeHandler(IBaseRepository<AppointmentNoticeModel> appointmentNoticeRepo,ILogger<AppointmentNoticeHandler> logger)
        {
            this.appointmentNoticeRepo = appointmentNoticeRepo;
            this.logger = logger;
        }
        /// <summary>
        /// 预约须知查询实现方法
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回同意包装类</returns>
        public async Task<ApiResult<AppointmentNoticeModel>> Handle(AppointmentNoticeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var appointment = appointmentNoticeRepo.GetAll().OrderByDescending(x=>x.Id).FirstOrDefault();
                if (appointment == null)
                {
                    return new ApiResult<AppointmentNoticeModel> {Msg="未查到数据",Code = ApiEnum.Fail };
                }
                return new ApiResult<AppointmentNoticeModel> {Msg= "预约须知查询成功",Code = ApiEnum.Success,Data = appointment };
            }
            catch (Exception ex)
            {
                logger.LogError("预约查询发生异常"+ex.Message);
                return new ApiResult<AppointmentNoticeModel> { Msg = "预约须知查询发生异常"+ex.Message, Code = ApiEnum.Error};
            }
        }
    }
}
